#region Using Statements
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace SkyBurner.Screens
{
    public enum SkyBurnerScreenHiScoresMode
    {
        View,
        Input
    };

    public class SkyBurnerScreenHiScores : SkyBurnerScreenBase
    {
        private enum Buttons
        {
            PadButtonA,
            PadButtonB,
            PadButtonY
        };

        private SkyBurnerScreenHiScoresMode m_showMode;
        private char[] m_hiscoreName = new char[3];
        private char m_currentChar;
        private int m_nCurrentCharPos;
        private int m_nNewHiscore;
        private Texture2D[] m_buttonsTexture = new Texture2D[3];

        public SkyBurnerScreenHiScoresMode Mode
        {
            get { return m_showMode; }
            set { m_showMode = value; }
        }

        public int NewHiscore
        {
            get { return m_nNewHiscore; }
            set { m_nNewHiscore = value; }
        }

        public SkyBurnerScreenHiScores(BaseGame game, ScreenManager pScreenManager, int nScreenID, bool bActive)
            : base(game, pScreenManager, nScreenID, bActive)
        {
            // setup texture...
            m_showMode = SkyBurnerScreenHiScoresMode.View;
            m_nNewHiscore = 0;
            m_screenTexture = m_Game.ContentManager.Load<Texture2D>("Data\\Screens\\SkyBurnerMainScreen2");

            // load buttons texture
            m_buttonsTexture[(int)Buttons.PadButtonA] = m_Game.ContentManager.Load<Texture2D>("Data\\Textures\\GUI\\Pad_Button_A");
            m_buttonsTexture[(int)Buttons.PadButtonB] = m_Game.ContentManager.Load<Texture2D>("Data\\Textures\\GUI\\Pad_Button_B");
            m_buttonsTexture[(int)Buttons.PadButtonY] = m_Game.ContentManager.Load<Texture2D>("Data\\Textures\\GUI\\Pad_Button_Y");
        }

        public override void Activate()
        {
            base.Activate();
          
            m_currentChar = 'a';
            m_nCurrentCharPos = 0;
        }

        private void SaveHiscore(String pHiscoreName)
        {
            HiscoreTable hiscoreTable = HiscoreTable.Instance();
            try
            {
                hiscoreTable.AddScore(m_nNewHiscore, new String(m_hiscoreName));
                hiscoreTable.Save("Data\\Config\\Hiscores.xml");
            }
            catch (Exception e)
            {
                m_Game.Console.Print("Exception in saving hiscore : " + e.Message);
            }
        }

        public override void Update(GameTime gameTime, Camera pActiveCamera)
        {
            base.Update(gameTime, pActiveCamera);

            switch (m_showMode)
            {
                case SkyBurnerScreenHiScoresMode.View:
                    {
                        if (m_Game.UserInputState.IsUserAction(UserAction.ButtonA) || m_Game.UserInputState.IsUserAction(UserAction.Back))
                        {
                            m_Game.SoundManager.PlaySound("Highlight");
                            m_screenManager.ActivateScreen(SkyBurnerScreenID.sbs_Main);
                        }
                        break;
                    }

                case SkyBurnerScreenHiScoresMode.Input:
                    {
                        if (m_Game.UserInputState.IsUserAction(UserAction.Left))
                        {
                            if (m_currentChar > 'a')
                                m_currentChar--;
                        }
                        else if (m_Game.UserInputState.IsUserAction(UserAction.Right))
                        {
                            if (m_currentChar < 'z')
                                m_currentChar++;
                        }
                        else if (m_Game.UserInputState.IsUserAction(UserAction.ButtonA))
                        {
                            if (m_nCurrentCharPos < 3)
                            {
                                m_nCurrentCharPos++;
                                m_currentChar = 'a';
                            }
                            else
                            {
                                SaveHiscore(new String(m_hiscoreName));
                                m_showMode = SkyBurnerScreenHiScoresMode.View;
                            }
                        }
                        else if (m_Game.UserInputState.IsUserAction(UserAction.ButtonB))
                        {
                            if ((m_nCurrentCharPos > 0) && (m_nCurrentCharPos < 3))
                            {
                                m_hiscoreName[m_nCurrentCharPos--] = ' ';
                                m_currentChar = m_hiscoreName[m_nCurrentCharPos];
                            }
                            else if (m_nCurrentCharPos >= 3)
                                m_currentChar = m_hiscoreName[--m_nCurrentCharPos];
                            
                            return;
                        }
                        else if (m_Game.UserInputState.IsUserAction(UserAction.ButtonY))
                        {
                            SaveHiscore(new String(m_hiscoreName));
                            m_showMode = SkyBurnerScreenHiScoresMode.View;
                        }
                        else if (m_Game.UserInputState.IsUserAction(UserAction.Back))
                        {
                            SaveHiscore("abc");
                            m_showMode = SkyBurnerScreenHiScoresMode.View;
                        }

                        if (m_nCurrentCharPos < 3)
                            m_hiscoreName[m_nCurrentCharPos] = m_currentChar;

                        break;
                    }
            }
        }

        public override void Draw(GameTime gameTime, Camera pActiveCamera)
        {
            base.Draw(gameTime, pActiveCamera);
           
            m_Game.BitmapFont.PrintCenterX(40, "sky burner   hi score", 32);            
            switch (m_showMode)
            {
                case SkyBurnerScreenHiScoresMode.View:
                    {
                        HiscoreTable hiscoreTable = HiscoreTable.Instance();

                        for (int i = 0; i < hiscoreTable.ItemCount; i++)
                        {
                            HiscoreItem hiscoreItem = hiscoreTable.GetAt(i);
                            m_Game.BitmapFont.PrintCenterX(120 + i * 25, String.Format("{0}     {1,6}", hiscoreItem.Name, hiscoreItem.Score), 16);
                        }
                        break;
                    }

                case SkyBurnerScreenHiScoresMode.Input:
                    {
                        m_Game.BitmapFont.PrintCenterX(100, "congratulation", 32);
                        m_Game.BitmapFont.PrintCenterX(140, "insert you name", 32);

                        m_Game.BitmapFont.PrintCenterX(200, new String(m_hiscoreName), 32);
                      
                        // draw buttons...
                        m_Game.SpriteBatch.Begin();
                        m_Game.SpriteBatch.Draw(m_buttonsTexture[(int)Buttons.PadButtonY], new Rectangle(140, 240, 32, 32), Color.White);
                        m_Game.SpriteBatch.Draw(m_buttonsTexture[(int)Buttons.PadButtonA], new Rectangle(340, 240, 32, 32), Color.White);
                        m_Game.SpriteBatch.Draw(m_buttonsTexture[(int)Buttons.PadButtonB], new Rectangle(540, 240, 32, 32), Color.White);
                        m_Game.SpriteBatch.End();

                        // draw buttons labels...
                        m_Game.BitmapFont.Print(180, 250, "Confirm", 16);
                        m_Game.BitmapFont.Print(380, 250, "Insert", 16);
                        m_Game.BitmapFont.Print(580, 250, "Delete", 16);
                        break;
                    }
            }

        }
    }
}
